#include "h_stm32_iic.h"

#include "h_stm32_hal_defines.h"
#include <stdio.h>
#include <string.h>

//////////////////////////////////////////////////////////////////////////////////
#define h_stm32_hal_iic                                                 hi2c1
extern I2C_HandleTypeDef                                                h_stm32_hal_iic;
#define IIC_TIMEOUT                                                     0xFFFF
//////////////////////////////////////////////////////////////////////////////////

signed char h_stm32_iic_write(unsigned char addr, unsigned char reg, unsigned char *data, unsigned short len)
{
    addr <<= 1;
    return HAL_I2C_Mem_Write(&h_stm32_hal_iic, (unsigned short)addr, (unsigned short)reg, 1, data, len, IIC_TIMEOUT);
}

signed char h_stm32_iic_read(unsigned char addr, unsigned char reg, unsigned char *data, unsigned short len)
{
    addr <<= 1;
    return HAL_I2C_Mem_Read(&h_stm32_hal_iic, (unsigned short)addr, (unsigned short)reg, 1, data, len, IIC_TIMEOUT);
}

void h_stm32_iic_test(void)
{
    unsigned char data[5];
    unsigned char addr = 0;
    for(; addr < 255; addr++) {
        if(!h_stm32_iic_read(addr, 0x00, data, 1)) {
            printf("Get 0x%02x\r\n", addr);
        } else {
            printf(".");
        }
    }
}
